import * as THREE from "three";
import * as dat from "dat.gui";
// 导入轨道控制器
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";

const gui = new dat.GUI();
// 目标：灯光与阴影
// -- 材质要满足能够对光照反应
// a. 开启渲染器阴影的计算 renderer.shadowMap.enabled = true;
// b. 开启光照投射阴影 directionalLight.castShadow = true;
// c. 物体投射阴 spher.castShadow = true;
// d. 物体接收阴影 plane.receiveShadow = true;

// 1.创建场景
const scene = new THREE.Scene();
// 2. 创建相机
const camera = new THREE.PerspectiveCamera(
  75,
  window.innerWidth / window.innerHeight,
  0.1,
  1000
);

// 设置相机位置
camera.position.set(0, 0, 10);
// 添加相机到场景中
scene.add(camera);

// 创建球体
const sphereTexture = new THREE.SphereGeometry(1, 20, 20);
// 材质
const material = new THREE.MeshStandardMaterial({
  // metalness: 0.7,
  // roughness: 0,
  // envMap: envMapTexture,
});

// 关联几何体和材质
const spher = new THREE.Mesh(sphereTexture, material);
// c.物体投射阴影
spher.castShadow = true;

// 添加几何体到场景
scene.add(spher);

// 添加平面
const planeGeometry = new THREE.PlaneGeometry(30, 30);
const plane = new THREE.Mesh(planeGeometry, material);
plane.position.set(0, -1, 0);
plane.rotation.x = -Math.PI / 2;
// d. 物体接收阴影
plane.receiveShadow = true;
scene.add(plane);

// 灯光
// 添加环境光
const light = new THREE.AmbientLight(0x404040); // soft white light

scene.add(light);

// 直线光源(平行光)
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.5);
// 设置直线光源的照射位置
directionalLight.position.set(10, 10, 10);
// b.设置光照投射阴影
directionalLight.castShadow = true;

// 设置阴影贴图模糊度
directionalLight.shadow.radius = 20;
// 设置阴影贴图的分辨率
directionalLight.shadow.mapSize.set(4096, 4096);
// 设置平行光投射相机属性
// directionalLight.shadow.camera.near = 0.5;
// directionalLight.shadow.camera.far = 500;
// directionalLight.shadow.camera.top = 5;
// directionalLight.shadow.camera.bottom = -5;
// directionalLight.shadow.camera.left = -5;
// directionalLight.shadow.camera.right = 5;

// scene.add(directionalLight);

// gui
//   .add(directionalLight.shadow.camera, "near")
//   .min(0)
//   .max(20)
//   .step(0.1)
//   .onChange(() => {
//     directionalLight.shadow.camera.updateProjectionMatrix();
//   });

// 聚光灯光源
const spotLight = new THREE.SpotLight(0xffffff, 0.5);
// 设置直线光源的照射位置
spotLight.position.set(5, 5, 5);
// b.设置光照投射阴影
spotLight.castShadow = true;
// 设置阴影贴图模糊度
spotLight.shadow.radius = 20;
// 设置阴影贴图的分辨率
spotLight.shadow.mapSize.set(4096, 4096);
// 设置聚光灯朝向为球体spher
spotLight.target = spher;
gui.add(spher.position, "x").min(-20).max(20).step(0.1);

scene.add(spotLight);

// 4. 初始化渲染器
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
// a.开启场景中的阴影贴图
renderer.shadowMap.enabled = true;
// 将webgl渲染的canvas内容添加到body
document.body.appendChild(renderer.domElement);

const controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;

// 添加坐标轴辅助器
const axesHelper = new THREE.AxesHelper(5);
scene.add(axesHelper);

function animate() {
  controls.update();
  renderer.render(scene, camera);
  // 渲染下一帧的时候就调用animate
  requestAnimationFrame(animate);
}
animate();

// 监听画面变化
window.addEventListener("resize", () => {
  const iwidth = window.innerWidth;
  const iheigt = window.innerHeight;
  //更新摄像头
  camera.aspect = iwidth / iheigt;
  // 更新摄像机投影矩阵
  camera.updateProjectionMatrix();
  // 更新渲染器
  renderer.setSize(iwidth, iheigt);
  // 设置渲染器像素比
  renderer.setPixelRatio(window.devicePixelRatio);
});
